<%@page contentType="text/html" language="java" import="com.compuvest.web.*,java.util.*,java.lang.Integer,java.text.SimpleDateFormat,java.text.DecimalFormat" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<%
    final Map<String,Clickstream> clickstreams = (Map<String,Clickstream>)application.getAttribute("context.clientList");
    DecimalFormat pcf = new DecimalFormat("#0.00");
%>
<html>
<head><title>Admin</title>
<link rel="stylesheet" href="css/main.css" type="text/css">
</head>
<body style="margin:0 0;padding:0 0;background-color:#b0c4de;">

<table border=0 cellspacing=1 cellpadding=1 bgcolor=white>
 <tr><th>#</th><th>ip address</th><th>clicks</th><th>session time</th><th>last click ago</th><th>advert</th><th>agent</th></tr>
<%
    int count = 0;
    Map<String,Integer> agents = new HashMap<String,Integer>();

    Iterator it = clickstreams.keySet().iterator();
    while(it.hasNext()) {
	String key = (String)it.next();
	Clickstream stream = (Clickstream)clickstreams.get(key);
	count++;

	if(agents.containsKey(stream.getAgent())) {
	    int cnt = (Integer)agents.get(stream.getAgent());
	    agents.put(stream.getAgent(), ++cnt);
	}
	else {
	    agents.put(stream.getAgent(), 1);
	}

	try {
%>
 <tr bgcolor='<%= (count % 2 == 0)? "#d3d3d3": "#dcdcdc" %>' align=center>
  <td><%= count %></td>
  <td align=left><a href='viewsession.jsp?sid=<%= key %>'><b><%= stream.getHostname() %></b></a></td>
  <td><%= stream.getStream().size() %></td>
<%
    long streamLenght = stream.getLastRequest().getTime() - stream.getStartRequest().getTime();
    long streamOld = new java.util.Date().getTime() - stream.getLastRequest().getTime();

    String ltime =  (streamLenght > 3600000? " " + (streamLenght / 3600000) + "h": "") +
    		    (streamLenght > 60000? " " + ((streamLenght / 60000) % 60) + "m": "") +
    		    (streamLenght > 1000? " " + ((streamLenght / 1000) % 60) + "s": "");

    String otime =  (streamOld > 3600000? " " + (streamOld / 3600000) + "h": "") +
    		    (streamOld > 60000? " " + ((streamOld / 60000) % 60) + "m": "") +
    		    (streamOld > 1000? " " + ((streamOld / 1000) % 60) + "s": "");
%>

  <td align=right><%= ltime %></td>
  <td align=right><%= otime %></td>
  <td align=left><%= stream.getAdvertSite() %></td>
  <td align=left><%= stream.getAgent() %></td>
 </tr>
<%
	}
	catch(Exception e) {
%>
 An error occurred - <%= e.getMessage() %><br/>
<%
	}
    }
%>
</table>
<br/>

<table border=0 cellspacing=1 cellpadding=1 bgcolor=white>
 <tr><th>count</th><th>agent</th><th>%</th></tr>
<%
    Iterator ait = agents.keySet().iterator();
    while(ait.hasNext()) {
	String key = (String)ait.next();
	int cnt = (Integer)agents.get(key);
%>
 <tr bgcolor=#d3d3d3><td><%= cnt %></td><td><%= key %><td><%= pcf.format((double)cnt / (double)count * 100) %>%</td></tr>
<%
    }
%>
</table>

</body>
</html>
